<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.mousewheel.js"></script>
    <script src="js/scroll.js"></script>
    <title>聊天机器人</title>
  </head>

  <body>
    <div class="wrap">
      <!-- 头部 Header 区域 -->
      <div class="header">
        <h3>小思同学</h3>
        <img src="img/person01.png" alt="icon" />
      </div>
      <!-- 中间 聊天内容区域 -->
      <div class="main">
        <ul class="talk_list" style="top: 0px" id="talk_list">
          <li class="left_word">
            <img src="img/person01.png" /> <span>嗨，最近想我没有？</span>
          </li>
          <!-- <li class="right_word">
          <img src="img/person02.png" /> <span>你好哦</span>
        </li> -->
        </ul>
        <div class="drag_bar" style="display: none">
          <div
            class="drager ui-draggable ui-draggable-handle"
            style="display: none; height: 412.628px"
          ></div>
        </div>
      </div>
      <!-- 底部 消息编辑区域 -->
      <div class="footer">
        <img src="img/person02.png" alt="icon" />
        <input
          type="text"
          placeholder="说的什么吧..."
          class="input_txt"
          id="ipt"
        />
        <input type="button" value="发 送" class="input_sub" id="btnSend" />
      </div>
    </div>
    <!-- 注意：只要为 audio 指定了新的 src 属性，而且指定了 autoplay，那么，语音就会自动播放 -->
    <audio src="" id="voice" autoplay style="display: none"></audio>

    <script>
      // 1 给我们btnSend 绑定点击事件  把输入框里面的内容展示带页面上
     $(function(){
      var baseUrl = 'http://www.liulongbin.top:3006';

      $('#btnSend').click(function(){
        var ipt = $('#ipt').val().trim();
        if(!ipt){
          alert('输入值不能为空')
          return false
        }
        $('#talk_list').append($(`<li class="right_word">
        <img src="img/person02.png" /> <span>${ipt}</span>
        </li>`))
          resetui()
        $.ajax({
            url : baseUrl + '/api/robot',
            type : 'get',
            data:{
              spoken : ipt,
            },
            beforeSend :function(){
              $('#ipt').val('')//成功了之后清空输入框里的值
            },
            success:function(res){
              //  console.log(res.data.info.text)
              var robotTxt = res.data.info.text;

              $('#talk_list').append($(`<li class="left_word">
            <img src="img/person01.png" /> <span>${robotTxt}</span>
          </li>`))

          resetui()

          $.ajax({
            url : baseUrl + '/api/synthesize',
            type : 'get',
            data:{
              text : robotTxt
            },
            success : function(res){
              console.log(res);
              if(res.status == 200){
                $('#voice').attr('src',res.voiceUrl)
              }
            },

          })
            },
        })
      })

    $('#ipt').on('keyup',function(e){
      if(e.keyCode === 13){
        $('#btnSend').click()
      }
    })




     })
    </script>
  </body>
</html>
